package com.ccl.iot.object;

public class IOTRegData extends IOTRegInfo {
    public byte iData[];

    public int iNetWorkState = -1;

    public IOTRegData() {

    }

    public IOTRegData(IOTRegData aInfo) {
        super(aInfo);
        if (aInfo != null) {
            setData(aInfo.iData, aInfo.iSize);
        }
    }

    public IOTRegData(long aDevID, int aAddress, int aSize, byte aData[]) {
        super(aDevID, aAddress, aSize);

        setData(aData, aSize);
    }

    //===============modify start======================
    public IOTRegData(long aDevID, int aAddress, int aSize, byte aData[], int netWorkState) {
        this(aDevID, aAddress, aSize, aData);
        iNetWorkState = netWorkState;
    }
    //===============modify end======================

    @Override
    public boolean isValid() {
        if (super.isValid()) {
            return iData != null && iData.length == iSize;
        } else {
            return false;
        }
    }

    public boolean setData(byte aData[], int aSize) {
        if (aData != null && aSize > 0 && aData.length >= aSize) {
            iData = new byte[aSize];

            System.arraycopy(aData, 0, iData, 0, aSize);

            iSize = aSize;

            return true;
        }

        return false;
    }

    public boolean replaceData(byte aData[], int aAddress, int aSize) {
        if (aData != null && aSize > 0 && aData.length >= aSize && iData != null && aAddress >= iAddress && (aAddress + aSize) <= (iAddress + iSize)) {
            /*boolean tRet = false;
            for (int i = 0; i < aSize; i++) {
                if (iData[aAddress + i] != aData[i]) {
                    iData[aAddress + i] = aData[i];

                    tRet = true;
                }
            }

            return tRet;*/

            for (int i = 0; i < aSize; i++) {
                iData[aAddress + i] = aData[i];
            }
            return true;
        }

        return false;
    }

    public boolean expand(int aAddress, int aSize, byte aData[]) {
        byte tData[] = null;

        if (aAddress == iAddress + iSize) {
            tData = new byte[iSize + aSize];

            if (iData != null) {
                System.arraycopy(iData, 0, tData, 0, iSize);
            }
            if (aData != null) {
                System.arraycopy(aData, 0, tData, iSize, aSize);
            }
        } else if (iAddress == aAddress + aSize) {
            tData = new byte[iSize + aSize];

            if (aData != null) {
                System.arraycopy(aData, 0, tData, 0, aSize > aData.length ? aData.length : aSize);
            }

            if (iData != null) {
                System.arraycopy(iData, 0, tData, aSize, iSize);
            }
        }

        if (tData != null) {
            if (aAddress < iAddress) {
                iAddress = aAddress;
            }

            iSize += aSize;
            iData = tData;

            return true;
        }

        return false;
    }

    @Override
    public boolean expand(int aAddress, int aSize) {
        if (aSize > 0) {
            return expand(aAddress, aSize, new byte[aSize]);
        } else {
            return false;
        }
    }

    @Override
    public IOTRegData clone() {
        return new IOTRegData(this);
    }
}
